package com.lixin.web.webSocket.impl;

import com.lixin.exception.DuplicateLikeException;
import com.lixin.exception.ParamsException;
import com.lixin.model.request.LikeRequest;
import com.lixin.model.response.Response;
import com.lixin.redis.RedisKey;
import com.lixin.redis.SetRedisUtil;
import com.lixin.service.ILikesService;
import com.lixin.web.netty.NettyUtil;
import com.lixin.web.webSocket.LikeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Description:
 * Copyright:   Copyright (c)2023
 * Company:     sci
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2023-08-13 19:35:03
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2023-08-13     张李鑫                     1.0         1.0 Version
 */
@Service("like")
public class LikeManagerImpl implements LikeManager {
    @Autowired
    private SetRedisUtil setRedisUtil;

    @Autowired
    private ILikesService iLikesService;

    @Override
    public Response<Object> like(LikeRequest likeRequest) {
        Integer postId = likeRequest.getPostId();
        if (postId == null) {
            throw new ParamsException();
        }
        Integer userId = NettyUtil.getUserId();
        if (setRedisUtil.sIsMember(RedisKey.getKey(RedisKey.POST_LIST, postId), userId)) {
            throw new DuplicateLikeException();
        }
        iLikesService.likePost(likeRequest.getPostId(), userId);
        setRedisUtil.sAdd(RedisKey.getKey(RedisKey.POST_LIST, postId),userId);
        return Response.success();
    }
}
